﻿
Imports System.IO


Public Class WriteLog
    Public Enum ErrorType
        ErrorOnly = 1
        DebugStatementsOnly = 2
        Both = 3
    End Enum

    Public Sub WriteToErrorLog(ByVal msg As String, _
         ByVal stkTrace As String, ByVal title As String, ByVal ErrorT As ErrorType)
        Dim cReg As New CReg
        Try

            WriteToEventLog("Message" & vbCrLf & "ex.ToString", , EventLogEntryType.Error)
            Dim sFileName As String = cReg.ReadValue(cReg.HKeyCurrentUser, "Software\Global Visas\CRM3\Custom", "LogFileName", "")
            Dim sPath As String = cReg.ReadValue(cReg.HKeyCurrentUser, "Software\Global Visas\CRM3\Custom", "LogPath", "")
            Dim LogValue As String = cReg.ReadValue(cReg.HKeyCurrentUser, "Software\Global Visas\CRM3\Custom", "LogValue", "")
            Dim LogType As ErrorType
            If Not LogValue = String.Empty Then
                LogType = CType(cReg.ReadValue(cReg.HKeyCurrentUser, "Software\Global Visas\CRM3\Custom", "LogValue", ""), ErrorType)
            End If
            If LogType = ErrorT Then

                If Not System.IO.Directory.Exists(sPath) Then
                    System.IO.Directory.CreateDirectory(sPath)
                End If
                If Right(sPath, 1) <> "\" Then
                    sPath = sPath & "\"
                End If
                'check the file
                Dim fs As FileStream = New FileStream(sPath & sFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)

                Dim s As StreamWriter = New StreamWriter(fs)
                s.Close()
                fs.Close()

                'log it
                Dim fs1 As FileStream = New FileStream(sPath & sFileName, FileMode.Append, FileAccess.Write)
                Dim s1 As StreamWriter = New StreamWriter(fs1)
                's1.Write("Title: " & title & vbCrLf)
                's1.Write("Message: " & msg & vbCrLf)
                's1.Write("StackTrace: " & stkTrace & vbCrLf)
                s1.Write(DateTime.Now.ToString() & "     " & msg & "  " & stkTrace)
                s1.Close()
                fs1.Close()
            End If
        Catch ex As Exception
            WriteToEventLog(ex.Message & vbCrLf & ex.ToString, , EventLogEntryType.Error)
            MsgBox("Fatal Error in writing the log file. Check event log for more information.", MsgBoxStyle.Critical, "Error")
        End Try

    End Sub
    Public Function WriteToEventLog(ByVal entry As String, _
                    Optional ByVal appName As String = "Global Visas CRM", _
                    Optional ByVal eventType As  _
                    EventLogEntryType = EventLogEntryType.Information, _
                    Optional ByVal logName As String = "CRM3") As Boolean

        Dim objEventLog As New EventLog

        Try

            'Register the Application as an Event Source
            If Not EventLog.SourceExists(appName) Then
                EventLog.CreateEventSource(appName, logName)
            End If

            'log the entry
            objEventLog.Source = appName
            objEventLog.WriteEntry(entry, eventType)

            Return True

        Catch Ex As Exception

            Return False

        End Try

    End Function

End Class
